// regex 正则表达式的应用
// Created by Administrator on 2025/6/26.
//

#include <iostream>
#include <regex>
using namespace std;

/**
 * 匹配
 * 检查字符串是否匹配字符串
 */
void matchFunc() {
    const string text = "hello,world!";
    const regex pattern("^[a-zA-Z]+,[a-zA-Z]+!$");

    if (regex_match(text, pattern)) {
        cout << "字符串匹配" << endl;;
    } else {
        cout << "字符串不匹配" << endl;
    }
}

/**
 * 搜索
 * 在字符串中搜索匹配项
 */
void searchFunc() {
    string txt = "123-456-7890 and 987-654-3210";
    const regex pattern("\\d{3}-\\d{3}-\\d{4}");

    smatch matches;
    while (regex_search(txt, matches, pattern)) {
        cout << "found:" << matches[0] << endl;
        txt = matches.suffix().str();
    }
}

/**
 * 替换
 * 替换字符串中的匹配项
 */
void replaceFunc() {
    const string txt = "hello world";
    const regex pattern("world");
    const string replaceContent = "c++";
    const string result = regex_replace(txt, pattern, replaceContent);
    cout << "source content:\t" << txt << endl;
    cout << "new content:\t" << result << endl;
}


int main(int argc, char *argv[]) {
    // matchFunc();
    // searchFunc();
    replaceFunc();
}
